6  Data types, Operators, Statements, and Structures in R

6.1 Data Types in R

Data types refer to the kind of data that can be stored and manipulated within a program. In R, the basic data types include:

  • Numeric: Represents real numbers (e.g., 2, 15.5).
  • Integer: Represents whole numbers (e.g., 2L, where L denotes an integer).
  • Character: Represents strings (e.g., “hello”, “1234”). Character must be put between “.
  • Logical: Represents Boolean values (TRUE or FALSE).

6.1.1 Assigning Values and Basic Operations

Assignment Operator

  • The assignment operator in R is used to assign values to variables or objects in the R programming language.
  • The leftwards assignment operator <-: This is the most commonly used assignment operator in R. It assigns the value on its right to the object on its left. For example, x <- 3 assigns the value 3 to the variable x.
  • Alternative Assignment Operator (=) Apart from <-, R also supports the use of the = operator for assignments, similar to many other programming languages.
  • However, the use of <- is preferred in R for historical and readability reasons. For example, x = 3 is valid but x <- 3 is more idiomatic to R.

Use <- or = for assigning values, e.g., x <- 10 or x= 10

Code
x=5
y=3
x+y
[1] 8

Commenting Code for Clarity

Use # for comments, e.g., # This is a comment.  - Comments are not executable and are used to provide relevant information about the syntax. Whatever is typed after # symbol, is considered as comment.

Code
# Multiply two variables.
x=5
y=3
x*y
[1] 15

Arithmetic operators

  • In R, arithmetic operators are used to perform common mathematical operations on numbers, vectors, matrices, and arrays. Here’s an overview of the primary arithmetic operators available in R: +, -, *, /, ^

Division (/) operator - Divides the first number or vector by the second, element-wise.

Code
x=5
y=3
x/y
[1] 1.666667

Square (^) operator - Squares the first number by the second.

Code
x=5
x^2
[1] 25

6.2 Statements

6.2.1 Logical Operations

Includes ==, !=, >, <, >=, <=.

Equality: == checks if two values are equal.

Code
# Equality 5 == 3
x <- 5
y <- 3
x == y  
[1] FALSE

Inequality: != checks if two values are not equal.

Code
# Inequality 5 != 3
x != y  
[1] TRUE

Greater than: > checks if the value on the left is greater than the value on the right.

Code
# Greater than 5 > 3
x > y   
[1] TRUE

Less than: < checks if the value on the left is less than the value on the right.

Code
# Less than 5 < 3
x < y   
[1] FALSE

Greater than or equal to: >= checks if the value on the left is greater than or equal to the value on the right.

Code
# Greater than or equal to 5 >= 3
x >= y  
[1] TRUE

Less than or equal to: <= checks if the value on the left is less than or equal to the value on the right.

Code
# Less than or equal to 5 <= 3
y <= x 
[1] TRUE

6.3 Data Structures

Vectors

  • Vectors are fundamental data structures that hold elements of the same type.
  • They are one-dimensional arrays that can store numeric, character, or logical data.
  • Assigning data to vectors in R is a basic operation, essential for data manipulation and analysis.
  • The c() function combines values into a vector. It’s the most common method for creating vectors.
Code
creating vector
# Numeric vector
age <- c(20, 21, 23)
age
[1] 20 21 23
Code
# Character vector
student <- c("ajay", "vijay", "jay")
student
[1] "ajay"  "vijay" "jay"  
Code
# logical vector
Pass<- c(TRUE, FALSE, TRUE) 
Pass
[1]  TRUE FALSE  TRUE

Matrix

  • A two-dimensional, rectangular collection of elements of the same type.
  • All elements must be of the same data type.
  • Created using the matrix() function. nrow is used to set number of rows and byrow is used to set values by rows (if TRUE) or columns (if FALSE).
Code
# Create a matrix with 2 rows and 3 columns
my_matrix <- matrix(data = c(1, 2, 3, 4, 5, 6), nrow = 2, ncol = 3, byrow = TRUE)

# Print the matrix
print(my_matrix)
     [,1] [,2] [,3]
[1,]    1    2    3
[2,]    4    5    6

Array

  • Similar to matrices but can have more than two dimensions.
  • Elements within an array must all be of the same data type.
  • Created using the array() function. dimensions are set using dim.
Code
# Create a 3-dimensional array with dimensions 2x3x2
my_array <- array(data = c(1:24), dim = c(2, 3, 4))

# Print the array
print(my_array)
, , 1

     [,1] [,2] [,3]
[1,]    1    3    5
[2,]    2    4    6

, , 2

     [,1] [,2] [,3]
[1,]    7    9   11
[2,]    8   10   12

, , 3

     [,1] [,2] [,3]
[1,]   13   15   17
[2,]   14   16   18

, , 4

     [,1] [,2] [,3]
[1,]   19   21   23
[2,]   20   22   24